package com.kgc.community.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.community.mapper.EquipmentMapper;
import com.kgc.community.mapper.RoomsMapper;
import com.kgc.community.mapper.UserMapper;
import com.kgc.community.mapper.VillageMapper;
import com.kgc.community.pojo.*;
import com.kgc.community.service.DeviceService;
import com.kgc.community.service.VillageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author dingChangXiao
 * @create 2020-11-29  17:02  com.kgc.community.service.impl  null.java
 */
@Service
public class DeviceServiceImpl implements DeviceService {
    @Resource
    VillageMapper villageMapper;


    @Resource
    EquipmentMapper equipmentMapper;


    @Override
    public PageInfo<Equipment> selectAllDevice(String s, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id desc");
        EquipmentExample example = new EquipmentExample();
        if(startTime!="" & endTime!=""){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date sTime = sdf.parse(startTime);
                Date eTime = sdf.parse(endTime);
                example.createCriteria().andBuyTimeBetween(sTime,eTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        EquipmentExample.Criteria criteria2 = example.createCriteria();
        if(!s.equals("%%")){
            criteria2.andNameLike(s);
        }
        example.or(criteria2);
        List<Equipment> equipments = equipmentMapper.selectByExample(example);
        PageInfo<Equipment> pageInfo=new PageInfo<>(equipments);
        return pageInfo;
    }

    @Override
    public int deleteDevice(Integer id) {
        Integer count = equipmentMapper.deleteByPrimaryKey(id);
        return count;
    }

    @Override
    public List<Village> selectVillageName() {
        List<Village> villages = villageMapper.selectByExample(null);
        return villages;
    }

    @Override
    public int addDevice(Equipment equipment) {
        Integer count = equipmentMapper.insertSelective(equipment);
        return count;
    }

    @Override
    public Equipment selectDevice(Integer equipmentId) {
        EquipmentExample example = new EquipmentExample();
        EquipmentExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(equipmentId);
        List<Equipment> equipments = equipmentMapper.selectByExample(example);
        if( equipments !=null){
            for(Equipment equipment: equipments ){
                return equipment;
            }
        }
        return null;
    }

    @Override
    public int modifyDevice(Equipment equipment) {
        Integer count = equipmentMapper.updateByPrimaryKeySelective(equipment);
        return count;
    }


}
